<?php

namespace App\Common\Provider\Uploader;

use App\Common\Exception\ApiMessageException;
use App\Common\Util\TP;
use think\facade\Filesystem;
use think\file\UploadedFile;

/**
 * Class LocalUploader
 */
class LocalUploader implements UploaderInterface
{

    public function upload(UploadedFile $uploadedFile): UploadResult
    {
        if (!in_array(strtolower($uploadedFile->getOriginalExtension()), TP::config('filesystem.disks.public.allow_suffix'))) {
            throw new ApiMessageException('该文件类型不允许上传');
        }

        $res = Filesystem::disk('public')->putFile("local", $uploadedFile, 'md5');
        $res = TP::config('filesystem.disks.public.url') . $res;

        return new UploadResult($res, $uploadedFile->getOriginalName());
    }
}